class MarketController < ApplicationController

	before_filter :login_required
	# model :trigger
    def index
    @stocks = Stock.open_stocks
	end
	
  def show
    @stock = Stock.find(params[:id])
    @buy_triggers = Trigger.filter(@stock.id, "buy")
	@sell_triggers = Trigger.filter(@stock.id, "sell")

  rescue
  	not_existing(id)
  end
  def buy
  	stock = Stock.find(params[:id])
  	@opening = stock.days_opening
  	@trigger = Trigger.new
	@trigger.user_id = current_user.id
	@trigger.stock_id = stock.id
	@trigger.price = stock.market_value
	@trigger.quantity = 100
	@trigger.action = 'buy'
  end
  def sell
  	stock = Stock.find(params[:id])
  	@opening = stock.days_opening
  	@trigger = Trigger.new
	@trigger.user_id = current_user.id
	@trigger.stock_id = stock.id
	@trigger.price = stock.market_value
	@trigger.quantity = 100
	@trigger.action = 'sell'
  end
  
    def sell_all
  	stock = Stock.find(params[:id])
  	@opening = stock.days_opening
  	@trigger = Trigger.new
	@trigger.user_id = current_user.id
	@trigger.stock_id = stock.id
	@trigger.price = stock.market_value
	@trigger.quantity = Ownership.owns(current_user.id, stock.id)
	@trigger.action = 'sell'
  end
	
end
